package eco2automotive.util;
import java.io.*;
import java.util.HashMap;
import java.util.ArrayList;

import eco2automotive.data.HashErrorCode;
import eco2automotive.data.HashParameters;
import eco2automotive.Log.LogInformation;
import eco2automotive.Log.Log;
/**  
 *  Imports specific data from the xml files: </br>
 * eco2automotive/data/*.xml </br>
 * and generate the files .dat
  * @version 1.0
  * @autor Fco. Javier Sánchez Esparza
  */ 
public class InitData implements Log{
    ImportXMLdata data;
    HashErrorCode herrorcode;
    LogInformation log=new LogInformation();
public String getLogInformation(){return log.getLogInformation();}
/**
 * 
 * @param source
 * @return table of protocols
 */
public ArrayList loadDataProtocol(String source){
ArrayList a=new ArrayList();
   File f = new File(source);
log.addLogInformation(InitData_Statics.INITDATA_Open_File+source);

if(f.exists()) {
	try {
            FileInputStream fileIn = new FileInputStream(source);
	    ObjectInputStream in = new ObjectInputStream( fileIn );
              a=(ArrayList)in.readObject();
              in.close();	
            log.addLogInformation(InitData_Statics.INITDATA_Open_File_OK);
        } 
    	catch( Exception evt ){log.addLogInformation(InitData_Statics.INITDATA_Open_File_ERROR);}	
}
else{log.addLogInformation(InitData_Statics.INITDATA_Open_File_ERROR2+source);}     
return a;
}
/**
 * 
 * @param source
 * @return HashMap of trouble codes 
 */
public  HashMap loadDataTroubleCodes(String source){
HashMap hash;
hash=null;
File f = new File(source);
log.addLogInformation(InitData_Statics.INITDATA_Open_File+source);
if(f.exists()) {
	try {
            FileInputStream fileIn = new FileInputStream(source);
	    ObjectInputStream in = new ObjectInputStream( fileIn );
              hash=(HashMap)in.readObject();
              in.close();	
            log.addLogInformation(InitData_Statics.INITDATA_Open_File_OK);
        } 
    	catch( Exception evt ){log.addLogInformation(InitData_Statics.INITDATA_Open_File_ERROR);}	
}
else{log.addLogInformation(InitData_Statics.INITDATA_Open_File_ERROR2+source);}
return hash;
}
/**
 * 
 * @param source
 * @return HashMap load a object HashMap from a file
 */
public HashMap loadData(String source){
HashMap hash;
hash=null;
File f = new File(source);
log.addLogInformation(InitData_Statics.INITDATA_Open_File+source);
if(f.exists()) {
	try {
            FileInputStream fileIn = new FileInputStream(source);
	    ObjectInputStream in = new ObjectInputStream( fileIn );
              hash=(HashMap)in.readObject();
              in.close();	
            log.addLogInformation(InitData_Statics.INITDATA_Open_File_OK);
        } 
    	catch( Exception evt ){log.addLogInformation(InitData_Statics.INITDATA_Open_File_ERROR);}	
}
else{log.addLogInformation(InitData_Statics.INITDATA_Open_File_ERROR2+source);}
return hash;
}
/**
 * 
 * @param source
 * @return ArrayList
 */
public ArrayList loadDataOperationModes(String source){
ArrayList a=new ArrayList();
   File f = new File(source);
log.addLogInformation(InitData_Statics.INITDATA_Open_File+source);

if(f.exists()) {
	try {
            FileInputStream fileIn = new FileInputStream(source);
	    ObjectInputStream in = new ObjectInputStream( fileIn );
              a=(ArrayList)in.readObject();
              in.close();	
            log.addLogInformation(InitData_Statics.INITDATA_Open_File_OK);
        } 
    	catch( Exception evt ){log.addLogInformation(InitData_Statics.INITDATA_Open_File_ERROR);}	
}
else{log.addLogInformation(InitData_Statics.INITDATA_Open_File_ERROR2+source);}     
return a;
}
/**
 * 
 * @param sourceXML
 * @param directory
 */
public void generateFileTroubleCodes(String sourceXML,String directory){ 
    // sourceXML: Archivo fuente del XML; directory: directorio destino
    //Genera el Archivo y lo salva(saveFile)
        HashMap hash;   
        data = new ImportXMLdata();
        data.parseXml(sourceXML);
        log.addLogInformation(InitData_Statics.INITDATA_Create_FILE+
                InitData_Statics.INITDATA_NAMEof_ErrorTroubleCodes_DAT);
        
        this.herrorcode=data.parseTroubleCodes();  
        hash=herrorcode.getHashErrorCode(); // Me devuelve la tabla generado a partir de XML 
        //Guardamos la tabla como HashMap        
        log.addLogInformation(InitData_Statics.INITDATA_SavingFile+
                directory);
       saveFile(hash,directory+InitData_Statics.INITDATA_NAMEof_ErrorTroubleCodes_DAT);
}
/**
 * 
 * @param sourceXML
 * @param directory
 */
public void generateFileProtocolTable(String sourceXML,String directory){ 
 data = new ImportXMLdata();
 data.parseXml(sourceXML);
 
 log.addLogInformation(InitData_Statics.INITDATA_Create_FILE+
                InitData_Statics.INITDATA_NAMEof_Protocols_DAT);
 log.addLogInformation(InitData_Statics.INITDATA_SavingFile+
                directory);
 saveFile(data.parseTableOfProtocols(),directory+InitData_Statics.INITDATA_NAMEof_Protocols_DAT);
}
/**
 * 
 * @param sourceXML
 * @param directory
 */
public void generateFileParameterTable(String sourceXML,String directory){ 
 data = new ImportXMLdata();
 data.parseXml(sourceXML);
 log.addLogInformation(InitData_Statics.INITDATA_Create_FILE+
                InitData_Statics.INITDATA_NAMEof_ParametersPIDs_DAT);
 log.addLogInformation(InitData_Statics.INITDATA_SavingFile+
                directory);
 HashParameters h=data.parseTableOfParameter();
  
 saveFile(h.getHash(),directory+InitData_Statics.INITDATA_NAMEof_ParametersPIDs_DAT);
}
/**
 * 
 * @param sourceXML
 * @param directory
 */
public void generateFileOperationMode(String sourceXML,String directory){
 data = new ImportXMLdata();
 data.parseXml(sourceXML);
 
 log.addLogInformation(InitData_Statics.INITDATA_Create_FILE+
                InitData_Statics.INITDATA_NAMEof_OperationModes_DAT);
 log.addLogInformation(InitData_Statics.INITDATA_SavingFile+
                directory);
 saveFile(data.parseTableOfProtocols(),directory+InitData_Statics.INITDATA_NAMEof_OperationModes_DAT);
}
/**
 * 
 * @param h
 * @param source
 */
public void saveFile(Object h, String source){
try {
    FileOutputStream fOut = new FileOutputStream(source);
    ObjectOutput out = new ObjectOutputStream(fOut);
    out.writeObject(h);
    out.flush();
    out.close();
    			}	
 catch( IOException ex ) {
  log.addLogInformation(InitData_Statics.INITDATA_SavingFile_ERROR+source);
  ex.printStackTrace();
 }			
log.addLogInformation(InitData_Statics.INITDATA_SavingFile_OK+source);
//System.exit(0);	
}
/**
 * 
 * @param args
 */
public static void main( String[] args ) {
InitData i = new InitData();
String directory=new WorkingDirectory().workingDirectoryJar().replace('\\', '/');
String source;   
String sourceXML;;  

//Generate INITDATA_NAMEof_Protocols_DAT
 source=InitData_Statics.INITDATA_Directory_of_DATA_+InitData_Statics.INITDATA_NAMEof_Protocols_DAT;   
 sourceXML=InitData_Statics.INITDATA_Directory_of_DATA_+InitData_Statics.INITDATA_NAMEof_Protocols_XML;  
 i.generateFileProtocolTable(directory+sourceXML, directory+InitData_Statics.INITDATA_Directory_of_DATA_);

 //Generate INITDATA_NAMEof_ErrorTroubleCodes_DAT
 source=InitData_Statics.INITDATA_Directory_of_DATA_+InitData_Statics.INITDATA_NAMEof_ErrorTroubleCodes_DAT; 
 sourceXML=InitData_Statics.INITDATA_Directory_of_DATA_+InitData_Statics.INITDATA_NAMEof_ErrorTroubleCodes_XML;
 i.generateFileTroubleCodes(directory+sourceXML, directory+InitData_Statics.INITDATA_Directory_of_DATA_);
 
  //Generate INITDATA_NAMEof_ParametersPIDs_DAT
 source=InitData_Statics.INITDATA_Directory_of_DATA_+InitData_Statics.INITDATA_NAMEof_ParametersPIDs_DAT; 
 sourceXML=InitData_Statics.INITDATA_Directory_of_DATA_+InitData_Statics.INITDATA_NAMEof_ParametersPIDs_XML;
 i.generateFileParameterTable(directory+sourceXML, directory+InitData_Statics.INITDATA_Directory_of_DATA_);    
 
 
 //Generate INITDATA_NAMEof_OperationModes_DAT (for future use)
 source=InitData_Statics.INITDATA_Directory_of_DATA_+InitData_Statics.INITDATA_NAMEof_OperationModes_DAT; 
 sourceXML=InitData_Statics.INITDATA_Directory_of_DATA_+InitData_Statics.INITDATA_NAMEof_OperationModes_XML;
 i.generateFileOperationMode(directory+sourceXML, directory+InitData_Statics.INITDATA_Directory_of_DATA_);
    
System.out.println(i.getLogInformation());   
}
}
